/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "Obstacle.h"
#include "World.h"
#include "owndebug.h"


CObject* CObstacle::LoadMLMA(CWorld *world, xmlNode *node)
{
  CObject *object=NULL;
  // Parameters for a Line.
  xmlChar *x = xmlGetProp(node, (xmlChar *) "x");
  xmlChar *y = xmlGetProp(node, (xmlChar *) "y");
  
  // Process.
  if (x && y) {
    object=new CObstacle(world, atof((const char*)x), atof((const char*)y));
  } else {
    dPrint(1,"MLMA Error:Not enough parameters defined for Obstacle-object!");
  }

  // Cleanup (Always do this to clean out properties which were found)
  if (x) xmlFree(x);
  if (y) xmlFree(y);
  
  if (object)
  {
    world->InsertObject(object);
  }
  
  return object;
}

CObstacle::CObstacle(CWorld* World, float x, float y)
  :CObject(World,NULL),numOfCollisions(0),colliding(false),
           primitive(NULL),lastCollider(NULL)
{
  
  material.surface.mu=5000;
  
  primitive=new CBox(0.7,0.7,0.7,this,this->SpaceID,0,&material);
  
  primitive->NotifyCollisions=true;
  
  dGeomSetPosition(primitive->GeomID, x,0.35,y);
  
  Parts.push_back(primitive);
}

CObstacle::~CObstacle()
{
  delete primitive;
}

void CObstacle::Activate()
{
  for (ObjectsIter i=Children.begin();i!=Children.end();i++)
  {
    (*i)->Activate();
  }
}

void CObstacle::Update(float StepSize)
{
  if (!colliding)
  {
    lastCollider=NULL;
    material.look.color[0]=numOfCollisions/30.0;
    material.look.color[1]=1.0-numOfCollisions/30.0;;
    material.look.color[2]=0;
  } else {
    material.look.color[0]=1.0;
    material.look.color[1]=0.5;
    material.look.color[2]=1.0;
  }
  colliding=false;
  for (ObjectsIter i=Children.begin();i!=Children.end();i++)
  {
    (*i)->Update(StepSize);
  }
}

void CObstacle::OnCollision(CPrimitive *primitive, int numc, dContact *contacts,
                             CPrimitive *primitive2)
{
  //if (!(SimWorld->GetTerrain()->IsTerrain(primitive2)))
  if (!primitive2->IsStatic)
  {
    colliding=true;
    if (lastCollider!=primitive2)
    {
      numOfCollisions++;
    }
    lastCollider=primitive2;
  }
}

void CObstacle::Draw()
{
  for (ObjectsIter i=Children.begin();i!=Children.end();i++)
  {
    (*i)->Draw();
  }

  for (PrimitivesIter i=Parts.begin();i!=Parts.end();i++)
  {
    (*i)->Draw();
  }

}

/******************************************************************************/


